package com.dao;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.entity.Users;
import com.utils.DBUtilt;

public class DBOperation<T> {
	private Class<T> entity;
	private String tableName;
	private Field[] fd;
	
	public DBOperation() {
		//获取当前dao对象的父类（参数化类型）
		ParameterizedType type=(ParameterizedType) this.getClass().getGenericSuperclass();
		entity=(Class<T>) type.getActualTypeArguments()[0];
		fd=entity.getDeclaredFields();
		tableName=entity.getSimpleName();
	}
	
	public List<Users> sqlQuery(T o){
		List<Users> list =new ArrayList<>();
		StringBuffer sql=new StringBuffer("select * from ");
		sql.append(tableName);
		System.out.println(sql.toString());
		try {
			PreparedStatement pstm=DBUtilt.getCon().prepareStatement(sql.toString());
			ResultSet rs=pstm.executeQuery();
			while(rs.next()){
				Users user=new Users();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setSex(rs.getString("sex"));
				list.add(user);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
